package wh.两句话中不常见的单词;

import com.sun.deploy.util.StringUtils;
import wh.实现strStr函数.Str;

import java.util.*;
import java.util.regex.Pattern;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2022/12/29 14:42
 * @description:
 * @version:
 */
public class Solution {
    public static void main(String[] args) {
        String s1 = "s z z z s";
        String s2 = "s z ejt";
        uncommonFromSentences(s1, s2);
    }


    //没必要这么麻烦，只需要建立一个hash就可以
    private static String[] uncommonFromSentences2(String s1, String s2) {
        String[] newStrArr1 = s1.split("\\s+");
        String[] newStrArr2 = s2.split("\\s+");
        Map<String,Integer> strMap1 = new HashMap();
        Map<String,Integer> strMap2 = new HashMap();

        //这种写法比下面的更加简洁
        for (String word : newStrArr1){
            strMap1.put(word,strMap1.getOrDefault(word,0) + 1);
        }

        for (int i = 0; i < newStrArr2.length; i++) {
            if (strMap2.containsKey(newStrArr2[i])){
                Integer num = strMap2.get(newStrArr2[i]);
                strMap2.put(newStrArr2[i],++num);
            } else {
                strMap2.put(newStrArr2[i],1);
            }
        }

        Set<String> set3 = strMap2.keySet();
        Iterator<String> iterator3 = set3.iterator();
        while (iterator3.hasNext()){
            String key = iterator3.next();
            if (strMap1.containsKey(key)){
                strMap1.remove(key);
                iterator3.remove();
            }
        }

        //使用迭代器删除map中value大于1的组合
        Set<String> set = strMap1.keySet();
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            if (strMap1.get(key) > 1){
                iterator.remove();
            }
        }

        Set<String> set2 = strMap2.keySet();
        Iterator<String> iterator2 = set2.iterator();
        while (iterator2.hasNext()){
            String key = iterator2.next();
            if (strMap2.get(key) > 1){
                iterator2.remove();
            }
        }


        String[] strings = strMap1.keySet().toArray(new String[strMap1.size()]);
        String[] strings2 = strMap2.keySet().toArray(new String[strMap2.size()]);
        String[] result = new String[strings.length+strings2.length];
        for (int i = 0; i < strings.length; i++) {
                result[i] = strings[i];
        }
        for (int i = strings.length,j = 0; i < result.length; i++,j++) {
            result[i] = strings2[j];
        }
        return result;
    }

    public static String[] uncommonFromSentences(String s1, String s2) {
        Map<String, Integer> freq = new HashMap<String, Integer>();
        insert(s1, freq);
        insert(s2, freq);

        List<String> ans = new ArrayList<String>();
        for (Map.Entry<String, Integer> entry : freq.entrySet()) {
            if (entry.getValue() == 1) {
                ans.add(entry.getKey());
            }
        }
        return ans.toArray(new String[0]);
    }

    public static void insert(String s, Map<String, Integer> freq) {
        String[] arr = s.split(" ");
        for (String word : arr) {
            freq.put(word, freq.getOrDefault(word, 0) + 1);
        }
    }

}
